package com.atguigu.gmall.pay.notify;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.atguigu.gmall.common.config.mq.service.MqService;
import com.atguigu.gmall.common.constant.MqConst;
import com.atguigu.gmall.pay.service.AlipayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-23 19:44
 **/
@Slf4j
@RequestMapping("/api/payment/alipay")
@RestController
public class TradeStatusNotifyController {

    @Autowired
    AlipayService alipayService;
    @Autowired
    MqService mqService;

    @PostMapping("/trade/notify")
    public String tradeStatus(@RequestParam Map<String,String> params) throws AlipayApiException {
        // 验证签名，获取支付宝POST过来反馈信息
        boolean signVerified = alipayService.checkSign(params);
        if (signVerified){
            // 验证成功
            log.info("验签通过，开始给订单服务发送已支付消息");
            // 给订单服务发送成功单的消息
            mqService.convertAndSend(MqConst.ORDER_EVENT_EXCHANGE,MqConst.ORDER_PAYED_RK,params);
        }else {
            return "非法操作";
        }
        log.info("异步通知抵达：携带参数是：{}",params);
        // 返回信息给支付宝
        return "success";
    }
}
